package com.sinai.mshab.data.gwt;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TextBoxBase;
import com.google.gwt.user.client.ui.Widget;
import com.sinai.mshab.data.core.DataManager;
import com.sinai.mshab.data.core.DataManagerCollection;

/**
 * A concrete implementation of {@link DataManagerCollection} that can return
 * {@link DataManager}s for GWT based widgets.
 * 
 * @author Arthur Kalmenson
 */
public class GwtDataManagerCollection implements DataManagerCollection {

	/**
	 * A map of GWT class names to {@link DataManager}s.
	 */
	private Map<String, DataManager<?, ?>> classnameToManager;

	/**
	 * Creates a new {@code GwtDataManagerCollection} and maps classnames to
	 * {@link DataManager}s.
	 */
	public GwtDataManagerCollection() {
		classnameToManager = new HashMap<String, DataManager<?, ?>>();

		// populate our classnameToManager with GWT widget managers.

		// for TextBoxBase widgets.
		DataManager<TextBoxBase, String> textBoxBaseDataManager =
				new TextBoxBaseDataManager();
		classnameToManager.put(TextBox.class.getName(), textBoxBaseDataManager);
		classnameToManager
				.put(TextArea.class.getName(), textBoxBaseDataManager);
		classnameToManager.put(PasswordTextBox.class.getName(),
				textBoxBaseDataManager);

		// for ListBox widgets.
		classnameToManager.put(ListBox.class.getName(),
				new ListBoxDataManager());
	}

	public DataManager<?, ?> getDataManager(Widget widget) {
		return classnameToManager.get(widget.getClass().getName());
	}

	public boolean hasDataManager(Widget widget) {
		return classnameToManager.containsKey(widget.getClass().getName());
	}

}
